package com.ficus.road.maintain.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ficus.road.maintain.core.model.CarConfig;
import com.ficus.road.maintain.core.model.carmanager.CarInfo;
import com.ficus.road.maintain.service.carmanager.CarInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/2/23 下午6:57
 */
@RestController
@RequiredArgsConstructor
public class ConfigController {
    private final CarInfoService carInfoService;

    @GetMapping("/config/{carId}")
    public ResponseEntity<String> getCarConfig(@PathVariable("carId") String carId) {
        CarInfo carInfo = carInfoService.findByCarId(carId);
        if (ObjectUtils.isEmpty(carInfo)) {
            return new ResponseEntity<>("error, carId is illegal", HttpStatus.BAD_REQUEST);
        }
        CarConfig config = carInfo.getConfig();
        ObjectMapper mapper = new ObjectMapper();
        String s = null;
        try {
            s = mapper.writeValueAsString(config);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(s, HttpStatus.OK);
    }

    @GetMapping("/dataFlush/config")
    public ResponseEntity<String> InsertConfig() {

        List<String> carList = Arrays.asList(
                "沪A-A28113", "沪A-DT5683", "沪A-DV5060", "沪A-DY5857", "沪A-A71989", "沪A-A92898"
        );

        List<List<Integer>> list = Arrays.asList(
                Arrays.asList(0, 0, 0, 0),
                Arrays.asList(114, 255, 120, 127),
                Arrays.asList(114, 255, 120, 178),
                Arrays.asList(114, 255, 120, 127),
                Arrays.asList(255, 253, 105, 127),
                Arrays.asList(255, 253, 105, 127),
                Arrays.asList(0, 122, 255, 127),
                Arrays.asList(255, 253, 105, 127),
                Arrays.asList(0, 122, 255, 127),
                Arrays.asList(0, 122, 255, 127),
                Arrays.asList(117, 117, 117, 127),
                Arrays.asList(105, 122, 0, 127)
        );
        CarConfig carConfig = new CarConfig();
        CarConfig.FrameImage frameImage = new CarConfig().new FrameImage();
        CarConfig.LidarView lidarView = new CarConfig().new LidarView();
        CarConfig.SegMask segMask = new CarConfig().new SegMask();
        CarConfig.FrameImage.EncodeParam encodeParam = new CarConfig().new FrameImage().new EncodeParam();
        CarConfig.FrameImage.EncodeParam.Option option = new CarConfig().new FrameImage().new EncodeParam().new Option();
        carConfig.setCarId("");
        carConfig.setCameraId(1);
        carConfig.setImageScale(0.3);
        carConfig.setOutputPointCloud(false);
        carConfig.setOutputFullGPS(true);
        carConfig.setOutputRawPicture(false);
        carConfig.setOutputRawSubPicture(false);
        carConfig.setOutputSegMaskPicture(false);
        carConfig.setSegUseImage(true);
        carConfig.setSegColorMap(list);
        frameImage.set_type("video");
        option.setPreset("slow");
        option.setTune("zerolatency");
        encodeParam.setOption(option);
        encodeParam.setCodecId("H264");
        encodeParam.setOutputFormat("mp4");
        encodeParam.setCodecName("libx264");
        encodeParam.setPixelFormat("YUV420P");
        encodeParam.setFps(12);
        encodeParam.setBitrate(400000);
        encodeParam.setGopSize(10);
        encodeParam.setMaxBFrames(0);
        encodeParam.setHeight(0);
        encodeParam.setWidth(0);
        encodeParam.setThreadCount(1);
        frameImage.setEncodeParam(encodeParam);
        carConfig.setFrameImage(frameImage);
        lidarView.set_type("image");
        lidarView.setImageSuffix(".jpg");
        lidarView.setImageQuality(30);
        carConfig.setLidarView(lidarView);
        segMask.set_type("rle");
        carConfig.setSegMask(segMask);
        carConfig.setOutputRawSubPictureIndexList(Arrays.asList(0, 1));
        String s = JSON.toJSONString(carConfig);
        System.out.println(s);
        String configStr =
                "{" +
                        "  \"car_id\": \"\"," +
                        "  \"camera_id\": 1," +
                        "  \"image_scale\": 0.3," +
                        "  \"output_point_cloud\": false," +
                        "  \"output_full_gps\": true," +
                        "  \"output_raw_picture\": false," +
                        "  \"output_raw_sub_picture\": true," +
                        "  \"output_seg_mask_picture\": false," +
                        "  \"seg_use_image\": true," +
                        "  \"seg_color_map\": [" +
                        "    [" +
                        "      0," +
                        "      0," +
                        "      0," +
                        "      0" +
                        "    ]," +
                        "    [" +
                        "      114," +
                        "      255," +
                        "      120," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      114," +
                        "      255," +
                        "      120," +
                        "      178" +
                        "    ]," +
                        "    [" +
                        "      114," +
                        "      255," +
                        "      120," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      255," +
                        "      253," +
                        "      105," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      255," +
                        "      253," +
                        "      105," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      0," +
                        "      122," +
                        "      255," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      255," +
                        "      253," +
                        "      105," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      0," +
                        "      122," +
                        "      255," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      0," +
                        "      122," +
                        "      255," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      117," +
                        "      117," +
                        "      117," +
                        "      127" +
                        "    ]," +
                        "    [" +
                        "      105," +
                        "      122," +
                        "      0," +
                        "      127" +
                        "    ]" +
                        "  ]," +
                        "  \"frame_image\": {" +
                        "    \"_type\": \"video\"," +
                        "    \"encode_param\": {" +
                        "      \"codec_id\": \"H264\"," +
                        "      \"output_format\": \"mp4\"," +
                        "      \"codec_name\": \"libx264\"," +
                        "      \"pixel_format\": \"YUV420P\"," +
                        "      \"fps\": 12," +
                        "      \"bitrate\": 400000," +
                        "      \"gop_size\": 10," +
                        "      \"max_b_frames\": 0," +
                        "      \"height\": 0," +
                        "      \"width\": 0," +
                        "      \"thread_count\": 1," +
                        "      \"option\": {" +
                        "        \"preset\": \"slow\"," +
                        "        \"tune\": \"zerolatency\"" +
                        "      }" +
                        "    }" +
                        "  }," +
                        "  \"lidar_view\": {" +
                        "    \"_type\": \"image\"," +
                        "    \"image_suffix\": \".jpg\"," +
                        "    \"image_quality\": 30" +
                        "  }," +
                        "  \"seg_mask\": {" +
                        "    \"_type\": \"rle\"" +
                        "  }," +
                        "  \"output_raw_sub_picture_index_list\": [" +
                        "    0," +
                        "    1" +
                        "  ]" +
                        "}";
        carInfoService.findAll().stream().forEach(car -> {
            carConfig.setDump(false);
            if (carList.contains(car.getLicensePlateNumber())) {
                System.out.println(car.getLicensePlateNumber());
                carConfig.setDump(true);
            }
            car.setConfig(carConfig);
            carInfoService.saveOrUpdate(car);
        });
        return new ResponseEntity<>("success", HttpStatus.OK);
    }
}
